package com.mapbased.wfw.components;

import java.io.*;
import java.util.*;

import org.dom4j.*;
import com.mapbased.wfw.*;
import com.mapbased.wfw.PageLoader.*;

/**
 * <p>Title: </p>
 *
 * <p>Description:Use ContentGetter if cross webapplition
 * ,else use this component </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class Use
    extends Component
{

    Page pageRef;
    String[] query[];
    /**
     * -1, doN't cache
     * 0,always cache
     * >0,the number of seconeds to cache;
     */
    int updateInterval=0;
    String cacheValue;
    long cacheTo=0;

    /**
     * a component can choose to parse an element or not.
     *
     * @param ele Element
     * @param lc PageLoader
     * @return Element or null:if wanna page loader parse the
     *   component,return the ele, else return null
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public Element parseXml(Element ele,LoadContext lc)
    {
        String page=ele.attributeValue("page");
        String[] ss=page.split("\\?");
        page=ss[0];
        if(ss.length == 2)
        {
            String p=ss[1];
            String pp[]=p.split("&");
            this.query=new String[pp.length][];
            for(int i=0;i < pp.length;i++)
            {

                this.query[i]=pp[i].split("=");
            }

        }
        this.updateInterval=Integer.parseInt(
            ele.attributeValue("update-interval","0"));

        pageRef=lc.loader.getPage(page,this.locale);

        return null;

    }

    /**
     * render
     *
     * @param context RenderContext
     * @throws IOException
     * @todo Implement this com.mapbased.wfw.Component method
     */

    public void render(RenderContext c)
        throws IOException
    {
        if(this.updateInterval < 0)
        {
            this.doRender(c);
            return;
        }
        if(this.updateInterval == 0)
        {
            if(this.cacheValue != null)
            {
                c.getOutputWriter().write(this.cacheValue);
                return;
            }
        }
        if(this.cacheTo - System.currentTimeMillis() > 0)
        {
            c.getOutputWriter().write(this.cacheValue);
            return;
        }

        LocalRenderContext lrc=new LocalRenderContext(c);
        this.doRender(lrc);
        this.cacheValue=lrc.toString();
        this.cacheTo=System.currentTimeMillis() + this.updateInterval * 1000;
        c.getOutputWriter().write(this.cacheValue);

    }

    public void doRender(RenderContext context)
        throws IOException
    {
        if(this.query != null)
        {
            for(int i=0;i < this.query.length;i++)
            {
                String[] s=this.query[i];
                if(s.length == 2)
                {
                    context.putParam(s[0],s[1]);
                }
            }

        }
        PageHandler old=context.getPageHandler();
        context.setPageHandler(pageRef.getPageHandler());

        pageRef.getPageHandler().defaultAction(context);

        pageRef.defalutRenderBehavior(context);

        context.setPageHandler(old);

    }

    /**
     * renderHeader
     *
     * @param context RenderContext
     * @param sb StringBuffer
     * @param m Map
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public void renderHeader(RenderContext context,StringBuffer sb,Map m)
    {
        if(this.pageRef != null)
        {
            Component[] headables=this.pageRef.getHeadables();
            for(int i=0;i < headables.length;i++)
            {
                headables[i].renderHeader(context,sb,m);
            }
        }

    }

    public boolean isHeadable()
    {
        return this.pageRef != null && this.pageRef.getHeadables().length > 0;
    }

}
